﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSMF.ValidateCode.lib;

namespace CSMF.ValidateCode
{
    public partial class GetCode : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string guid = Request["guid"];

            ClearOldValues();
            //生成4位随机数，并将其转为string型，传入session，4位数字由随  机数从1000-9999中提取。  
            string validateCode = string.Empty;
            Random rand = new Random();
            int codeValue = rand.Next(1000, 9999);
            validateCode = codeValue.ToString();
            var table = Utility.GetHashtable();
            if (table.ContainsKey(guid))
            {
                CodeValue value = (CodeValue)table[guid];
                value.CreateTime = DateTime.Now;
                value.Code = validateCode;
                table[guid] = value;
            }
            else
            {
                CodeValue value = new CodeValue();
                value.CreateTime = DateTime.Now;
                value.Code = validateCode;
                table.Add(guid, value);
            }

            Random rand1 = new Random();
            Random rand2 = new Random();
            int red = rand1.Next(256);
            int green = rand2.Next(256);
            int blue1 = (red + green > 400) ? 0 : 400 - red - green;
            int blue2 = (blue1 > 255) ? 255 : blue1;
            Color col = Color.FromArgb(red, green, blue2);

            int imageWidth = validateCode.Length * 13;
            Bitmap bmp = new Bitmap(imageWidth, 20);
            Graphics g = Graphics.FromImage(bmp);
            g.Clear(Color.White);
            g.DrawRectangle(new Pen(Color.LightGray, 1), 0, 0, imageWidth - 1, 19);
            Font font = new Font("Arial", 16);
            Brush brush = new SolidBrush(col);//col 为上文随机出的颜色  
            Point po = new Point(0, 0);
            g.DrawString(validateCode, font, brush, po);

            Response.ContentType = "image/jpeg";
            //MemoryStream ms = new MemoryStream();
            bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
            bmp.Dispose();
            g.Dispose();
            //return new FileStreamResult(ms, "image/jpeg");
        }

        /// <summary>
        /// 清除过期的键值对
        /// </summary>
        public void ClearOldValues()
        {
            Hashtable table = Utility.GetHashtable();
            if (table.Keys.Count <= 0)
            {
                // 没有内容，不需要清理。
                return;
            }

            ApplicationLog.Debug("table.cout is :" + table.Count);
            ApplicationLog.Debug("table.keys.cout is :" + table.Keys.Count);
            string[] keys = new string[table.Keys.Count];
            table.Keys.CopyTo(keys, 0);

            for (int i = 0; i < keys.Length; i++)
            {
                var key = keys[i];
                CodeValue value = (CodeValue)table[key];
                if (value != null)
                {
                    if (value.CreateTime.AddMinutes(1) < DateTime.Now)
                    {
                        table.Remove(key);
                    }
                }
            }
        }
    }
}